<?php
namespace app\web\controller;

use think\Db;

/**
 * 会员卡共享控制器
 * @package app\index\controller
 */
class ViperShare extends Auth
{
    public $noNeedLogin = '';


    /**
     * 获取会员卡类型共享列表
     */
    public function lists(){
        if($this->request->isPost()){
            $getData = $this->request->post();
            if(!isset($getData['limit']) || empty($getData['limit'])){
                $getData['limit'] = '10';
            }
            $data = Db::name('qt_viper_share')
                ->field('vs.id,s.name as old_store_name,t.name as old_type_name,new_viper_type_id,old_viper_type_id,vs.new_store_id,vs.old_store_id')
                ->alias('vs')
                ->leftJoin('qt_store s','s.id = vs.old_store_id')
                ->leftJoin('qt_viper_type t','t.id = vs.old_viper_type_id')
                ->where(['vs.new_store_id'=>$this->userInfo['store_id']])
                ->paginate($getData['limit'])->each(function ($item){
                    $item['new_type_name'] = Db::name('qt_viper_type')->where('id',$item['new_viper_type_id'])->value('name');
                    return $item;
                });
            $this->result('请求成功',$data,200);
        }
        $this->result('请求错误','',204);
    }




    /**
     * 保存会员卡类型共享列表
     */
    public function save(){
        if($this->request->isPost()){
            $data = $this->request->post('data');
            if(empty($data)){
                $this->result('缺少参数','',204);
            }
            $data = json_decode($data,true);
            foreach ($data as $key=>$value){
                $value['new_store_id'] = $this->userInfo['store_id'];
                Db::name('qt_viper_share')->where('id',$value['id'])->update($value);
            }
            $this->result('保存成功',$data,200);
        }
        $this->result('请求错误','',204);
    }


}
